$(document).ready(function() {
    $('input[id=query]').change(function(){
       setBodyHeight();
    });

    $('.search').click(function() {
     closeBox();
     displayLoading();
   });

    $(document).keyup(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code === 27) {closeBox();}
    });

    $('#query').select2({
        placeholder: "All Proteins",
        tokenSeparators: [";", " "],
        minimumInputLength: 3,
        ajax: {
            url: "/NCI60/Query/suggest",
            dataType: 'jsonp',
            quietMillis: 100,
            data: function (term, page) { // page is the one-based page number tracked by Select2
                return {
                    query: term, //search term
                    max: 10, // page size
                    offset: page // page number
                };
            },
            results: function (data, page) {
                var more = (page * 10) < data.total; // whether or not there are more results available

                // notice we return the value of more so Select2 knows if more results can be loaded
                return {results: data.elements, more: more};
            }
        },
        formatResult: queryFormatResult, // omitted for brevity, see the source of this page
        formatSelection: queryFormatSelection, // omitted for brevity, see the source of this page
        dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
        tags: ['']
    });
    $('.select2-default').css('width', '120px');

 });

 var curHistoSize = 110;
 var nextNotClicked = true;

function displayLoading() {
    $("#results").html('<div class="loading">Loading...</div>');
}

function queryFormatResult(element) {
        if(element.type=='uniprot'){element.show='Protein Name'}else{element.show=element.type}
        var markup = "<div class='result "+element.type+"'>";
        markup += "<div class='type'>"+element.show+":</div>";
        markup += "<div class='name'>"+element.name+"</div>";
        markup += "<div class='full'>"+element.full+"</div></div>";
        return markup;
    }

function queryFormatSelection(element) {
    return "<div class='"+element.type+"'>"+element.name+"</div>";
}

function setBodyHeight() {
    var topPos = $('.form .select2-container').height()+275;
    $('#results').css('top',topPos);
    var resHeight = $('#results').height();
    $('body').css('min-height', Math.max(920, topPos+resHeight+60));
    /*alert('top: '+topPos+' result: '+resHeight+' body: '+(topPos+resHeight+60));*/
}

function resizeResultTable() {
    var maxPlot = 100;
    $('.histo').each(function(){
        var t = $(this).width();
        maxPlot = (maxPlot>t)?maxPlot:t;
    });
    $('#results').width(725+maxPlot);
    $('body').css('min-width', Math.max(980, (725+maxPlot)*1.02));
}

function initResultTable(){
    setBodyHeight();
    
    nextNotClicked = true;
    $('#results .list').scroll(function(){
        
        if(isScrollBottomReached()&&nextNotClicked){
            $('.loadNext').click();
            $('.loadNext').parent('.listLine').html('<div class="loadNext element">...loading</div>');
            nextNotClicked = false;
        }
    });
}

function histoClickLink(id){
    $('#results .histo #'+id).click();
}

function isScrollBottomReached() {
    var divHeight = $('#results .list .data').height()+3;
    var scrollPosition = $('#results .list').height() + $('#results .list').scrollTop();
    return (divHeight == scrollPosition);
}

function closeBox() {
    $(".simpleExpressionPlots").remove();
}

/*for #draggable and resizable*/
function getTop(ele)
{
    var eTop = ele.offset().top;
    var wTop = $(window).scrollTop();
    var top = eTop - wTop;

    return top;
}

function setBoxSize(maxHeight){
    var winHeight = $(window).height();
    var maxHeightByWin = winHeight-180;
    var newHeight = Math.max(Math.min(maxHeight,maxHeightByWin),401);
    $(".simpleExpressionPlots").height(newHeight);
    $(".simpleExpressionPlots .projects").height(newHeight-30);
}
